<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="manager/css/font.css">
    <link rel="stylesheet" href="manager/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="manager/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="manager/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" action="roleList" method="post">
            <input type="hidden" name="pageNum" value="1" />
             <input type="hidden" name="pageSize" value="5" />
          <input type="text" name="rname" th:value="${rname}"  placeholder="请输入名称" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
          <button shiro:hasPermission="/delRoles" class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
          <button shiro:hasPermission="/roleAdd" class="layui-btn" onclick="x_admin_show('添加用户','roleAdd')"><i class="layui-icon"></i>添加</button>
          <span class="x-right" th:text="${'共有数据：'+pageInfo.total+ '条'}" style="line-height:40px"></span>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>
              <input type="checkbox" name="ckAll" >
            </th>
            <th>ID</th>
            <th>名称</th>
            <th>拥有权限规则</th>
            <th>描述</th>
            <th shiro:hasPermission="/updRoleStatus">状态</th>
            <th shiro:hasAnyPermissions="/updRoleStatus,/delRole,/roleEdit">操作</th>
        </thead>
        <tbody>
          <tr th:each="role:${pageInfo.list}">
            <td>
              <input type="checkbox" name="ck" th:value="${role.rid}" >
            </td>
            <td th:text="${role.rid}"></td>
            <td th:text="${role.rname}"></td>
              <td><span th:each="auth:${role.authss}" th:text="${auth.auname}"></span></td>
            <td th:text="${role.rdesc}"></td>
            <td shiro:hasPermission="/updRoleStatus" class="td-status">
              <span th:if="${role.rstatus==1}" class="layui-btn layui-btn-normal layui-btn-mini ">已启用</span>
              <span th:if="${role.rstatus!=1}" class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">已禁用</span>
            </td>

            <td shiro:hasAnyPermissions="/updRoleStatus,/delRole,/roleEdit" class="td-manage">
                <a shiro:hasPermission="/updRoleStatus" th:if="${role.rstatus==1}" th:onclick="member_stop(this,'[[${role.rid}]]')" href="javascript:;"  title="停用">
                    <i class="layui-icon">&#xe601; </i>
                </a>
                <a shiro:hasPermission="/updRoleStatus" th:if="${role.rstatus!=1}" th:onclick="member_stop(this,'[[${role.rid}]]')" href="javascript:;"  title="启用">
                <i class="layui-icon">&#xe62f; </i>
              </a>
              <a title="编辑" shiro:hasPermission="/roleEdit"  th:onclick="x_admin_show('编辑','roleEdit?rid=[[${role.rid}]]')" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
              </a>
              <a title="删除" shiro:hasPermission="/delRole" th:onclick="member_del(this,'[[${role.rid}]]')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="page">
          <div>
              <a class="prev" th:if="${pageInfo.pageNum>1}"  href="javascript:void(0)" th:onclick="ss([[${pageInfo.pageNum-1}]],[[${pageInfo.pageSize}]])" >&lt;&lt;</a>
              <span class="prev" th:if="${pageInfo.pageNum<=1}" >&lt;&lt;</span>
              <span class="num" th:text="${pageInfo.pageNum+'/'+pageInfo.pages}"></span>
              <a class="next"  th:if="${pageInfo.pageNum < pageInfo.pages}" href="javascript:void(0)" th:onclick="ss([[${pageInfo.pageNum+1}]],[[${pageInfo.pageSize}]])">&gt;&gt;</a>
              <span class="prev" th:if="${pageInfo.pageNum>=pageInfo.pages}" >>></span>
          </div>
      </div>

    </div>
    <script>
      layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

      function ss(pageNum,pageSize){
          $(":input[name='pageNum']").val(pageNum)
          $(":input[name='pageSize']").val(pageSize)
          $("form").submit();
      }

      $("input[name='ckAll']").click(function () {
        $("input[name='ck']").prop("checked",$("input[name='ckAll']").prop("checked"))
      })
      $("input[name='ck']").click(function () {
        var ss = $("input[name='ck']").length==$("input[name='ck']:checked").length;
        $("input[name='ckAll']").prop("checked",ss)
      })

      /*用户-停用*/
      function member_stop(obj,id){
        layer.confirm('确定要'+$(obj).attr('title')+'吗？',function(index){

          if($(obj).attr('title')=='停用'){
            $.ajax({
              url:"updRoleStatus",
              data:{"rid":id,"rstatus":2},
              type:"post",
              success:function (result) {
                if(result.result=="0000"){
                  $(obj).attr('title','启用')
                  $(obj).find('i').html('&#xe62f;');
                  $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                  layer.msg('已停用!',{icon: 5,time:1000});
                }else{
                  layer.msg("停用失败!",{icon:5,time:1000});
                }
              },
                error:function (result) {
                    layer.msg('权限不够!',{icon:2,time:1000});
                },
              dataType:"json"
            })
            //发异步把用户;状态进行更改
          }else{
            $.ajax({
              url:"updRoleStatus",
              data:{"rid":id,"rstatus":1},
              type:"post",
              success:function (result) {
                if(result.result=="0000"){
                  $(obj).attr('title','停用')
                  $(obj).find('i').html('&#xe601;');
                  $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                  layer.msg('已启用!',{icon: 6,time:1000});
                }else{
                  layer.msg("启用失败!",{icon:5,time:1000});
                }
              },
                error:function (result) {
                    layer.msg('权限不够!',{icon:2,time:1000});
                },
              dataType:"json"
            })
          }

        });
      }

      /*用户-删除*/
      function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
          $.ajax({
            url:"delRole",
            data:{"id":id},
            type:"post",
            success:function (result) {
              if(result.result=="0000"){
                $(obj).parents("tr").remove();
                layer.msg('已删除!',{icon:1,time:1000});
                location.reload()
              }else{
                layer.msg(result.resultMsg,{icon:2,time:1000});
              }
            },
              error:function (result) {
                  layer.msg('权限不够!',{icon:2,time:1000});
              },
            dataType:"json"
          })
        });
      }


      function delAll (argument) {

        var data = tableCheck.getData();

        layer.confirm('确认要删除吗？'+data,function(index){
          //捉到所有被选中的，发异步进行删除
          cks = $("input[name='ck']:checked");
          var str = "";
          for (var i = 0; i < cks.length;i++){
            str+=cks.eq(i).val()+",";
          }
          $.ajax({
            url:"delRoles",
            data:{"ids":str},
            type:"post",
            success:function (result) {
              if(result.result=="0000"){
                layer.msg('删除成功', {icon: 1});
                location.reload()
              }else{
                layer.msg(result.resultMsg,{icon:1,time:1000});
              }
            },
              error:function (result) {
                  layer.msg('权限不够!',{icon:2,time:1000});
              },
            dataType:"json"
          })
        });
      }
    </script>
  </body>

</html>